﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace NinjaToolbox.PhysicsSystem
{
    public abstract class ShapeSceneHandleDrawerBase
    {
        protected SerializedObject mSerializeObject;
        protected bool mIsInitialized;
        protected bool mEnabled;

        public bool IsInitialized { get { return mIsInitialized; } }
        public bool Enabled { get { return mEnabled; } set { mEnabled = value; } }


        /// <summary>
        /// 放到末尾处调用。
        /// </summary>
        public virtual void Initialization(SerializedObject serializeObject, IPhysicsShape shape)
        {
            mIsInitialized = true;
            mSerializeObject = serializeObject;

            SceneView.onSceneGUIDelegate += DrawSceneGUIBase;
        }

        /// <summary>
        /// 放在销毁处手动调用。
        /// </summary>
        public virtual void Dispose()
        {
            mIsInitialized = false;
            SceneView.onSceneGUIDelegate -= DrawSceneGUIBase;
        }

        public virtual void DrawInspectorGUI()
        {
            var rect = EditorGUILayout.GetControlRect();
            var value = GUI.Toggle(rect, mEnabled, "Enable Handle", EditorStyles.miniButton);

            if (value != mEnabled)
            {
                mEnabled = value;

                SceneView.RepaintAll();
            }
        }

        protected virtual void DrawSceneGUIBase(SceneView sceneView)
        {
            if (!mEnabled) return;

            DrawSceneGUI(sceneView);
        }

        protected virtual void DrawSceneGUI(SceneView sceneView)
        {

        }
    }
}
